import os

import cv2
# import numpy as np
from matplotlib import pyplot as plt
import pandas as pd


# from skimage import exposure

# 返回截取的污泥部分
def cutSludge(path,flag):
    # 高度绘图横坐标
    xaixs = []
    SumContourAreaList1 = []
    avgList1 = []
    contoursList1 = []
    SumRadioList1 = []

    data = pd.read_excel(path)
    for i in range(0, len(data['ASM']) - 1):
        xaixs.append(data['xaixs'][i])
        SumContourAreaList1.append(data['SumContourArea'][i])
        avgList1.append(data['avg'][i])
        contoursList1.append(data['num'][i])
        SumRadioList1.append(data['SumRadio'][i])








    if flag==0:

        plt.figure(figsize=(30,15))
        plt.plot(xaixs,SumContourAreaList1,linewidth=6)
        # plt.plot(xaixs,SumContourAreaList2,linewidth=6)
        # plt.plot(xaixs,SumContourAreaList3,linewidth=6)
        # plt.plot(xaixs,SumContourAreaList4,linewidth=6)

        # plt.plot(xaixs,avgList,linewidth=6)
        # plt.plot(xaixs,contoursList,linewidth=6)
        legendName = ['AAO-1','AAOYuan','YangHua1','YangHuaYuan']
        font1 = {'family': 'Times New Roman','weight': 'normal','size': 35}
        font2 = {'family': 'Times New Roman','weight': 'normal','size': 14}

        plt.tick_params(labelsize=25)
        plt.title('SumContourArea',fontsize=35, pad=40)
        #plt.legend(legendName,prop=font1)
        plt.xlabel('time/min', font1)
        plt.ylabel('SumContourArea/pixel', font1)
        plt.grid(True)

        plt.show()
    elif flag==1:

        plt.figure(figsize=(30,15))
        plt.plot(xaixs,avgList1,linewidth=6)
        # plt.plot(xaixs,avgList2,linewidth=6)
        # plt.plot(xaixs,avgList3,linewidth=6)
        # plt.plot(xaixs,avgList4,linewidth=6)

        # plt.plot(xaixs,avgList,linewidth=6)
        # plt.plot(xaixs,contoursList,linewidth=6)
        legendName = ['AAO-1','AAOYuan','YangHua1','YangHuaYuan']
        font1 = {'family': 'Times New Roman','weight': 'normal','size': 35}
        font2 = {'family': 'Times New Roman','weight': 'normal','size': 14}

        plt.tick_params(labelsize=25)
        plt.title('avg',fontsize=35, pad=40)
        plt.legend(legendName,prop=font1)
        plt.xlabel('time/min', font1)
        plt.ylabel('avg/pixel', font1)
        plt.grid(True)

        plt.show()
    elif flag==2:

        plt.figure(figsize=(30,15))
        plt.plot(xaixs,contoursList1,linewidth=6)
        # plt.plot(xaixs,contoursList2,linewidth=6)
        # plt.plot(xaixs,contoursList3,linewidth=6)
        # plt.plot(xaixs,contoursList4,linewidth=6)

        # plt.plot(xaixs,avgList,linewidth=6)
        # plt.plot(xaixs,contoursList,linewidth=6)
        legendName = ['AAO-1','AAOYuan','YangHua1','YangHuaYuan']
        font1 = {'family': 'Times New Roman','weight': 'normal','size': 35}
        font2 = {'family': 'Times New Roman','weight': 'normal','size': 14}

        plt.tick_params(labelsize=25)
        plt.title('contoursList',fontsize=35, pad=40)
        plt.legend(legendName,prop=font1)
        plt.xlabel('time/min', font1)
        plt.ylabel('contoursList/number', font1)
        plt.grid(True)

        plt.show()

    else:

        plt.figure(figsize=(30,15))
        plt.plot(xaixs,SumRadioList1,linewidth=6)
        # plt.plot(xaixs,SumRadioList2,linewidth=6)
        # plt.plot(xaixs,SumRadioList3,linewidth=6)
        # plt.plot(xaixs,SumRadioList4,linewidth=6)

        # plt.plot(xaixs,avgList,linewidth=6)
        # plt.plot(xaixs,contoursList,linewidth=6)
        legendName = ['AAO-1','AAOYuan','YangHua1','YangHuaYuan']
        font1 = {'family': 'Times New Roman','weight': 'normal','size': 35}
        font2 = {'family': 'Times New Roman','weight': 'normal','size': 14}

        plt.tick_params(labelsize=25)
        plt.title('AeraRadio',fontsize=35, pad=40)
        plt.legend(legendName,prop=font1)
        plt.xlabel('time/min', font1)
        plt.ylabel('AeraRadio/%', font1)
        plt.grid(True)

        plt.show()


    # 慎用 会plt很多图片 如果在jupyter里的话没关系
    # 下面的部分是用来观察所有图片处理中的情况
    # for i in range(60):
    #   preString = './imgCut/wushui/YangHua1/picture-'
    #   backString = '.jpg'
    #   mixPath = preString + str(i) + backString
    #   rea = CutSludge(mixPath)
    #   marked = rea.copy()
    #   gray = cv2.cvtColor(rea, cv2.COLOR_BGR2GRAY)
    #   # 长宽变化过程中 图片的大小面积实时计算
    #   sumAera = len(gray[0])*len(gray[0])
    #   print(sumAera)
    #   # gray = exposure.equalize_adapthist(gray, kernel_size=None, clip_limit=0.01, nbins=256)
    #   gray = skimage.img_as_ubyte(gray, force_copy=False)
    #   # ret2,th2 = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    #   # ret2,th2 = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.BINA)
    #
    #   # 此处adaptiveThreshold自适应二值化比OTSU二值化效果更好
    #   th2 = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,17,2)
    #   # th2 = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2) #换行符号 \
    #   # ret,th2=cv2.threshold(gray,50,255,cv2.THRESH_BINARY)
    #   # 要中值滤波 去除刻度线的干扰
    #   res = skimage.filters.median(th2, disk(3))
    #   contours, hierarchy = cv2.findContours(res,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    #   cv2.drawContours(marked,contours,-1,(0,0,255),1)
    #
    #   avg = 0
    #   SumContourArea = 0
    #   for i in contours:
    #     SumContourArea += cv2.contourArea(i)
    #   avg = SumContourArea/len(contours)
    #
    #   # 全局保存个数 总面积 平均面积
    #   SumContourAreaList2.append(SumContourArea)
    #   avgList2.append(avg)
    #   contoursList2.append(len(contours))
    #   SumRadioList2.append(SumContourArea/sumAera * 100)
    #
    #   plt.figure(figsize=(30, 4))
    #   plt.subplot(221)
    #   rea = rea[:,:,[2,1,0]]
    #   plt.imshow(rea,cmap='gray')
    #   plt.subplot(222)
    #   plt.imshow(th2,cmap='gray')
    #   plt.subplot(223)
    #   marked = marked[:,:,[2,1,0]]
    #   plt.imshow(marked,cmap='gray')
    #   plt.subplot(224)
    #   plt.imshow(res,cmap='gray')
    #   plt.show()